package com.zaofan.controller;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.Message;
import com.zaofan.bean.PageBean;
import com.zaofan.service.MessageService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

@WebServlet("/admin/message")
public class MessageController extends BaseServlet {
	private static final long serialVersionUID = -5377620282223515871L;
	private MessageService messageService = new MessageService();

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				if ("".equals(value)) {
					continue;
				}
				params.put(name, value);
			}
			String url = "message?action=list";
			if (!params.containsKey("begin")) {
				params.put("begin", "1");
			}
			if (!params.containsKey("end")) {
				params.put("end", "10");
			}
			params.put("order", " by addTime desc");
			PageBean pageBean = new PageBean();
			int page = Integer.valueOf((String) params.get("begin")) - 1;
			pageBean.setBegin(page * 10 + 1);
			params.put("begin", (page * 10) + "");
			params.put("order", " by addTime desc");
			List<Message> messages = messageService.findAll(params);
			Long count = messageService.count(params);
			pageBean.setEnd(page * 10 + messages.size());
			pageBean.setTotal(count.intValue());
			pageBean.setPageInfo(WebUtils.getPageData(count.intValue(), 10, page + 1, url));
			request.setAttribute("messages", messages);
			request.setAttribute("pageBean", pageBean);
			request.getRequestDispatcher("/admin/message/messageList.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	protected void to_add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			request.setAttribute("action", "add");
			request.getRequestDispatcher("/admin/message/messageAdd.jsp").forward(request, response);
		} catch (Exception e) {
		}
	}

	protected void to_edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Message message = messageService.findById(id);
			request.setAttribute("message", message);
			request.setAttribute("action", "update");
			request.getRequestDispatcher("/admin/message/messageAdd.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Message message = messageService.findById(id);
			JsonUtils.writeSuccessJson("查询成功", message, response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("查询失败!!", response);
	}

	protected void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Message message = WebUtils.write2bean(request, Message.class);
			messageService.delete(message);
			JsonUtils.writeSuccessJson("删除成功", "", response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("删除失败!!", response);
	}

	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Message message = WebUtils.write2bean(request, Message.class);
			messageService.add(message);
			JsonUtils.writeSuccessJson("添加成功!!", "", response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("属性错误!!", response);
		}
	}

	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Message message = WebUtils.write2bean(request, Message.class);
			messageService.updateMessage(message);
			JsonUtils.writeSuccessJson("更新成功!!", "", response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("更新失败!!", response);
		}
	}

	protected void state(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Message message = messageService.findById(id);
			message.setState(message.getState() == 0 ? 1 : 0);
			messageService.update(message);
			JsonUtils.writeSuccessJson("更新成功!!", "", response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("更新失败!!", response);
	}

}
